# Arbeitsverzeichnis festlegen
setwd("/Volumes/HDextern/Forschung/Habil/daten")
getwd()

# Datensatz einlesen
results <- read.csv("results.csv", header=TRUE, colClasses=c(rep("character",2),rep("numeric",23)), na.strings = "na")

# Zeiträume

periodQ  <- c("1Q1", "1Q2", "1Q3", "1Q4", "2Q1", "2Q2", "2Q3", "2Q4", "3Q1", "3Q2", "3Q3", "3Q4", "4Q1", "4Q2", "4Q3", "4Q4")

# Datensatz in Quartale, Perioden aufteilen
Q <- subset (results, subset = results$Date %in% periodQ)

### Periodisierung der Quartale und Perioden
Q[c("Period", "Group")] <- NA

per <- function(x) { 
  if(x == "1Q1" | x == "1Q2") y <- "P1"
  if(x == "1Q3" | x == "1Q4" | x == "2Q1" | x == "2Q2" | x == "2Q3" | x == "2Q4" | x == "3Q1") y <- "P2"
  if(x == "3Q2" | x == "3Q3" | x == "3Q4" | x == "4Q1" | x == "4Q2" | x == "4Q3" | x == "4Q4") y <- "P3"
  return(y)
}
Q$Period <- sapply(Q$Date,per)

Q[!is.na(Q[, "BACE"]), "limitBACE"] <- factor("below", levels=c("below", "between", "above"))
Q[which(Q[, "BACE"]>=0.32), "limitBACE"] <- "between"
Q[which(Q[, "BACE"]>0.4), "limitBACE"] <- "above"

Q[!is.na(Q[, "PWR"]), "limitPWR"] <- factor("below", levels=c("below", "between", "above"))
Q[which(Q[, "PWR"]>=0.2), "limitPWR"] <- "between"
Q[which(Q[, "PWR"]>0.28), "limitPWR"] <- "above"

Q[!is.na(Q[, "CC"]), "limitCC"] <- factor("below", levels=c("below", "between", "above"))
Q[which(Q[, "CC"]>=0.55), "limitCC"] <- "between"
Q[which(Q[, "CC"]>0.65), "limitCC"] <- "above"

Q[!is.na(Q[, "SC"]), "limitSC"] <- factor("below", levels=c("below", "between", "above"))
Q[which(Q[, "SC"]>=0.37), "limitSC"] <- "between"
Q[which(Q[, "SC"]>0.53), "limitSC"] <- "above"

Q[!is.na(Q[, "IGB"]), "limitIGB"] <- factor("below", levels=c("below", "between", "above"))
Q[which(Q[, "IGB"]>=0.10), "limitIGB"] <- "between"
Q[which(Q[, "IGB"]>0.16), "limitIGB"] <- "above"

Q[!is.na(Q[, "TASK"]), "limitTASK"] <- factor("below", levels=c("below", "between", "above"))
Q[which(Q[, "TASK"]>=0.56), "limitTASK"] <- "between"
Q[which(Q[, "TASK"]>0.68), "limitTASK"] <- "above"

Q[!is.na(Q[, "DIS"]), "limitDIS"] <- factor("below", levels=c("below", "between", "above"))
Q[which(Q[, "DIS"]>=0.09), "limitDIS"] <- "between"
Q[which(Q[, "DIS"]>0.15), "limitDIS"] <- "above"

# Datensatz in Akteure aufteilen
Armitage  <- subset (Q, Q$Actor=="Ar")
Bush  <- subset (Q, Q$Actor=="Bu")
Cheney  <- subset (Q, Q$Actor=="Ch")
Powell  <- subset (Q, Q$Actor=="Po")
Rice  <- subset (Q, Q$Actor=="Ri")
Rumsfeld  <- subset (Q, Q$Actor=="Ru")
Tenet  <- subset (Q, Q$Actor=="Te")
Wolfowitz  <- subset (Q, Q$Actor=="Wo")

# Export in CSV
#write.csv2(Armitage, file = "lta_Armitage.csv")
#write.csv2(Bush, file = "lta_Bush.csv")
#write.csv2(Cheney, file = "lta_Cheney.csv")
#write.csv2(Powell, file = "lta_Powell.csv")
#write.csv2(Rice, file = "lta_Rice.csv")
#write.csv2(Rumsfeld, file = "lta_Rumsfeld.csv")
#write.csv2(Tenet, file = "lta_Tenet.csv")
#write.csv2(Wolfowitz, file = "lta_Wolfowitz.csv")



require(ggplot2)
require(plyr)

### BACE
d_ply(Q, .(Actor), function(p){
  ggplot(p, aes(x = Date, y = BACE, colour=limitBACE))+
    geom_point(shape=15)+scale_colour_manual(values=c(between="#000000", above="#CC0000", below="#0066FF"))+
    ylim(0,0.6)+
    geom_hline(yintercept=0.32, linetype="dotdash")+
    geom_hline(yintercept=0.40, linetype="dotdash")+
    theme(legend.position = "none", panel.background = element_rect(fill = "#FFFFFF", colour="#000000"), panel.grid.major = element_line(colour = "grey", linetype = "dotted")) +
    facet_grid(.~ Period, scales="free", space="free")+
    #ggtitle(unique(p$Actor))+
    ggsave(file = paste0("lta_",unique(p$Actor),"_BACE", ".pdf"))
})


### PWR
d_ply(Q, .(Actor), function(p){
  ggplot(p, aes(x = Date, y = PWR, colour=limitPWR))+
    geom_point(shape=15)+scale_colour_manual(values=c(between="#000000", above="#CC0000", below="#0066FF"))+
    ylim(0,0.5)+
    geom_hline(yintercept=0.20, linetype="dotdash")+
    geom_hline(yintercept=0.28, linetype="dotdash")+
    theme(legend.position = "none", panel.background = element_rect(fill = "#FFFFFF", colour="#000000"), panel.grid.major = element_line(colour = "grey", linetype = "dotted")) +
    facet_grid(.~ Period, scales="free", space="free")+
    #ggtitle(unique(p$Actor))+
    ggsave(file = paste0("lta_",unique(p$Actor),"_PWR", ".pdf"))
})

### CC
d_ply(Q, .(Actor), function(p){
  ggplot(p, aes(x = Date, y = CC, colour=limitCC))+
    geom_point(shape=15)+scale_colour_manual(values=c(between="#000000", above="#CC0000", below="#0066FF"))+
    ylim(0.4,0.8)+
    geom_hline(yintercept=0.55, linetype="dotdash")+
    geom_hline(yintercept=0.65, linetype="dotdash")+
    theme(legend.position = "none", panel.background = element_rect(fill = "#FFFFFF", colour="#000000"), panel.grid.major = element_line(colour = "grey", linetype = "dotted")) +
    facet_grid(.~ Period, scales="free", space="free")+
    #ggtitle(unique(p$Actor))+
    ggsave(file = paste0("lta_",unique(p$Actor),"_CC", ".pdf"))
})

### SC
d_ply(Q, .(Actor), function(p){
  ggplot(p, aes(x = Date, y = SC, colour=limitSC))+
    geom_point(shape=15)+scale_colour_manual(values=c(between="#000000", above="#CC0000", below="#0066FF"))+
    ylim(0.2,0.8)+
    geom_hline(yintercept=0.37, linetype="dotdash")+
    geom_hline(yintercept=0.53, linetype="dotdash")+
    theme(legend.position = "none", panel.background = element_rect(fill = "#FFFFFF", colour="#000000"), panel.grid.major = element_line(colour = "grey", linetype = "dotted")) +
    facet_grid(.~ Period, scales="free", space="free")+
    #ggtitle(unique(p$Actor))+
    ggsave(file = paste0("lta_",unique(p$Actor),"_SC", ".pdf"))
})

### TASK
d_ply(Q, .(Actor), function(p){
  ggplot(p, aes(x = Date, y = TASK, colour=limitTASK))+
    geom_point(shape=15)+scale_colour_manual(values=c(between="#000000", above="#CC0000", below="#0066FF"))+
    ylim(0.4,0.8)+
    geom_hline(yintercept=0.56, linetype="dotdash")+
    geom_hline(yintercept=0.68, linetype="dotdash")+
    theme(legend.position = "none", panel.background = element_rect(fill = "#FFFFFF", colour="#000000"), panel.grid.major = element_line(colour = "grey", linetype = "dotted")) +
    facet_grid(.~ Period, scales="free", space="free")+
    #ggtitle(unique(p$Actor))+
    ggsave(file = paste0("lta_",unique(p$Actor),"_TASK", ".pdf"))
})

### DIS
d_ply(Q, .(Actor), function(p){
  ggplot(p, aes(x = Date, y = DIS, colour=limitDIS))+
    geom_point(shape=15)+scale_colour_manual(values=c(between="#000000", above="#CC0000", below="#0066FF"))+
    ylim(0,0.5)+
    geom_hline(yintercept=0.09, linetype="dotdash")+
    geom_hline(yintercept=0.15, linetype="dotdash")+
    theme(legend.position = "none", panel.background = element_rect(fill = "#FFFFFF", colour="#000000"), panel.grid.major = element_line(colour = "grey", linetype = "dotted")) +
    facet_grid(.~ Period, scales="free", space="free")+
    #ggtitle(unique(p$Actor))+
    ggsave(file = paste0("lta_",unique(p$Actor),"_DIS", ".pdf"))
})

### IGB
d_ply(Q, .(Actor), function(p){
  ggplot(p, aes(x = Date, y = IGB, colour=limitIGB))+
    geom_point(shape=15)+scale_colour_manual(values=c(between="#000000", above="#CC0000", below="#0066FF"))+
    ylim(0,0.3)+
    geom_hline(yintercept=0.10, linetype="dotdash")+
    geom_hline(yintercept=0.16, linetype="dotdash")+
    theme(legend.position = "none", panel.background = element_rect(fill = "#FFFFFF", colour="#000000"), panel.grid.major = element_line(colour = "grey", linetype = "dotted")) +
    facet_grid(.~ Period, scales="free", space="free")+
    #ggtitle(unique(p$Actor))+
    ggsave(file = paste0("lta_",unique(p$Actor),"_IGB", ".pdf"))
})

#########
########
######
####
###
##
#
# Bush - DIS
mean_DIS_Bush <- data.frame(Period = c("P1", "P2", "P3"),
                      yint = with(Bush,c(mean(DIS[1:2]), mean(DIS[3:9]), mean(DIS[10:16]))))

lta_bush_DIS <- ggplot(Bush, aes(x = Date, y = DIS, colour=Period))+
  geom_point(shape=15)+scale_colour_manual(values=c("#000000", "#CC0000", "#0066FF"))+
  ylim(0,0.5)+
  geom_hline(yintercept=0.09)+
  geom_hline(yintercept=0.15)+
  geom_hline(data = mean_DIS_Bush,aes(yintercept = yint),  linetype="dotdash")+
  theme(legend.position = "none", panel.background = element_rect(fill = "#FFFFFF", colour="#000000"), panel.grid.major = element_line(colour = "grey", linetype = "dotted")) +
  facet_grid(.~ Period, scales="free", space="free")
  #ggtitle("Distrust - Bush")

pdf("lta_Bush_DIS.pdf", paper='A4r', width=11)
lta_bush_DIS
dev.off()


# Cheney
for (i in c(Bush, Cheney, Powell, Armitage, Rumsfeld, Wolfowitz, Rice, Tenet)) {
mean_DIS_i <- data.frame(periods = c('1 - Quartale','2 - Perioden'),
                            yint = with(i,c(mean(DIS[1:16]),mean(DIS[17:19]))))

i$periods <- rep(c('1 - Quartale','2 - Perioden'),times = c(16,3))

lta_i_DIS <- ggplot(i, aes(x = Date, y = DIS))+
  geom_point(shape=16)+
  ylim(0,0.5)+
  geom_hline(yintercept=0.07)+
  geom_hline(yintercept=0.19)+
  geom_hline(data = mean_DIS_i,aes(yintercept = yint),  linetype="dotted")+
  theme(legend.position = "none")+
  facet_grid(.~ periods, scales="free", space="free")
  #ggtitle("Distrust -" i)

pdf(lta_i_DIS.pdf)
lta_i_DIS
dev.off()
}

# Powell
mean_DIS_Powell <- data.frame(periods = c('1 - Quartale','2 - Perioden'),
                              yint = with(Powell,c(mean(DIS[1:16]),mean(DIS[17:19]))))

Powell$periods <- rep(c('1 - Quartale','2 - Perioden'),times = c(16,3))

ggplot(Powell, aes(x = Date, y = DIS))+
  geom_point(shape=16)+
  ylim(0,0.5)+
  geom_hline(yintercept=0.07)+
  geom_hline(yintercept=0.19)+
  geom_hline(data = mean_DIS_Powell,aes(yintercept = yint),  linetype="dotted")+
  theme(legend.position = "none")+
  facet_grid(.~ periods, scales="free", space="free")+
  ggtitle("Distrust - Powell")

# Rumsfeld
mean_DIS_Rumsfeld <- data.frame(periods = c('1 - Quartale','2 - Perioden'),
                              yint = with(Rumsfeld,c(mean(DIS[1:16]),mean(DIS[17:19]))))

Rumsfeld$periods <- rep(c('1 - Quartale','2 - Perioden'),times = c(16,3))

ggplot(Rumsfeld, aes(x = Date, y = DIS))+
  geom_point(shape=16)+
  ylim(0,0.5)+
  geom_hline(yintercept=0.07)+
  geom_hline(yintercept=0.19)+
  geom_hline(data = mean_DIS_Rumsfeld,aes(yintercept = yint),  linetype="dotted")+
  theme(legend.position = "none")+
  facet_grid(.~ periods, scales="free", space="free")+
  ggtitle("Distrust - Rumsfeld")

# Rice
mean_DIS_Rice <- data.frame(periods = c('1 - Quartale','2 - Perioden'),
                                yint = with(Rice,c(mean(DIS[1:16]),mean(DIS[17:19]))))

Rice$periods <- rep(c('1 - Quartale','2 - Perioden'),times = c(16,3))

ggplot(Rice, aes(x = Date, y = DIS))+
  geom_point(shape=16)+
  ylim(0,0.5)+
  geom_hline(yintercept=0.07)+
  geom_hline(yintercept=0.19)+
  geom_hline(data = mean_DIS_Rice,aes(yintercept = yint),  linetype="dotted")+
  theme(legend.position = "none")+
  facet_grid(.~ periods, scales="free", space="free")+
  ggtitle("Distrust - Rice")

# Tenet
mean_DIS_Tenet <- data.frame(periods = c('1 - Quartale','2 - Perioden'),
                                yint = with(Tenet,c(mean(DIS[1:16]),mean(DIS[17:19]))))

Tenet$periods <- rep(c('1 - Quartale','2 - Perioden'),times = c(16,3))

ggplot(Tenet, aes(x = Date, y = DIS))+
  geom_point(shape=16)+
  ylim(0,0.5)+
  geom_hline(yintercept=0.07)+
  geom_hline(yintercept=0.19)+
  geom_hline(data = mean_DIS_Tenet,aes(yintercept = yint),  linetype="dotted")+
  theme(legend.position = "none")+
  facet_grid(.~ periods, scales="free", space="free")+
  ggtitle("Distrust - Tenet")

# Armitage
mean_DIS_Armitage <- data.frame(periods = c('1 - Quartale','2 - Perioden'),
                                yint = with(Armitage,c(mean(DIS[1:16]),mean(DIS[17:19]))))

Armitage$periods <- rep(c('1 - Quartale','2 - Perioden'),times = c(16,3))

ggplot(Armitage, aes(x = Date, y = DIS))+
  geom_point(shape=16)+
  ylim(0,0.5)+
  geom_hline(yintercept=0.07)+
  geom_hline(yintercept=0.19)+
  geom_hline(data = mean_DIS_Armitage,aes(yintercept = yint),  linetype="dotted")+
  theme(legend.position = "none")+
  facet_grid(.~ periods, scales="free", space="free")+
  ggtitle("Distrust - Armitage")

# Wolfowitz
mean_DIS_Wolfowitz <- data.frame(periods = c('1 - Quartale','2 - Perioden'),
                                yint = with(Wolfowitz,c(mean(DIS[1:16]),mean(DIS[17:19]))))

Wolfowitz$periods <- rep(c('1 - Quartale','2 - Perioden'),times = c(16,3))

ggplot(Wolfowitz, aes(x = Date, y = DIS))+
  geom_point(shape=16)+
  ylim(0,0.5)+
  geom_hline(yintercept=0.07)+
  geom_hline(yintercept=0.19)+
  geom_hline(data = mean_DIS_Wolfowitz,aes(yintercept = yint),  linetype="dotted")+
  theme(legend.position = "none")+
  facet_grid(.~ periods, scales="free", space="free")+
  ggtitle("Distrust - Wolfowitz")


### Darstellung der einzelnen LTA-Werte aller Akteure

lta_DIS <- ggplot(P, aes(x = Date, y = DIS, colour=factor(Date)))+
  geom_point(shape=16)+
  ylim(0,0.5)+
  geom_hline(yintercept=0.07, linetype="dotted")+
  geom_hline(yintercept=0.19, linetype="dotted")+
  facet_grid(.~ Actor, scales="free_x")+
  theme(legend.position = "none")+
  ggtitle("Distrust")

pdf("lta_DIS.pdf")
lta_DIS
dev.off()

lta_BACE <- ggplot(P, aes(x = Date, y = BACE, colour=factor(Date)))+
  geom_point(shape=16)+
  ylim(0.1,0.6)+
  geom_hline(yintercept=0.30, linetype="dotted")+
  geom_hline(yintercept=0.40, linetype="dotted")+
  facet_grid(.~ Actor, scales="free_x")+
  theme(legend.position = "none")+
  ggtitle("BACE")

pdf("lta_BACE.pdf")
lta_BACE
dev.off()

lta_PWR <- ggplot(P, aes(x = Date, y = PWR, colour=factor(Date)))+
  geom_point(shape=16)+
  ylim(0,0.4)+
  geom_hline(yintercept=0.21, linetype="dotted")+
  geom_hline(yintercept=0.31, linetype="dotted")+
  facet_grid(.~ Actor, scales="free_x")+
  theme(legend.position = "none")+
  ggtitle("PWR")

pdf("lta_PWR.pdf")
lta_PWR
dev.off()

lta_CC <- ggplot(P, aes(x = Date, y = CC, colour=factor(Date)))+
  geom_point(shape=16)+
  ylim(0.4,0.8)+
  geom_hline(yintercept=0.53, linetype="dotted")+
  geom_hline(yintercept=0.65, linetype="dotted")+
  facet_grid(.~ Actor, scales="free_x")+
  theme(legend.position = "none")+
  ggtitle("CC")

pdf("lta_CC.pdf")
lta_CC
dev.off()

lta_SC <- ggplot(P, aes(x = Date, y = SC, colour=factor(Date)))+
  geom_point(shape=16)+
  ylim(0.1,0.6)+
  geom_hline(yintercept=0.26, linetype="dotted")+
  geom_hline(yintercept=0.46, linetype="dotted")+
  facet_grid(.~ Actor, scales="free_x")+
  theme(legend.position = "none")+
  ggtitle("SC")

pdf("lta_SC.pdf")
lta_SC
dev.off()

lta_TASK <- ggplot(P, aes(x = Date, y = TASK, colour=factor(Date)))+
  geom_point(shape=16)+
  ylim(0.35,0.85)+
  geom_hline(yintercept=0.56, linetype="dotted")+
  geom_hline(yintercept=0.70, linetype="dotted")+
  facet_grid(.~ Actor, scales="free_x")+
  theme(legend.position = "none")+
  ggtitle("TASK")

pdf("lta_TASK.pdf")
lta_TASK
dev.off()

lta_IGB <- ggplot(P, aes(x = Date, y = IGB, colour=factor(Date)))+
  geom_point(shape=16)+
  ylim(0,0.4)+
  geom_hline(yintercept=0.10, linetype="dotted")+
  geom_hline(yintercept=0.20, linetype="dotted")+
  facet_grid(.~ Actor, scales="free_x")+
  theme(legend.position = "none")+
  ggtitle("IGB")

pdf("lta_IGB.pdf")
lta_IGB
dev.off()
